var xmlhttp;
var imported = document.createElement('script');
imported.src = 'checkLogin.php?id='+Math.random();
document.head.appendChild(imported);
function getxmlhttp(){
    if (window.XMLHttpRequest){			// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
    }
    else{								// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
	
	return xmlhttp;
}

// CASE 1
function ajaxModelsSeries(index, manufacturer_id, model_id, engine_id)
{
	if(check_login)
	{
		if(document.getElementById('model_series'))
			var models = document.getElementById('model_series');
		else
			var models = parent.document.getElementById('model_series');
		var i = 0;

		// Изчистване на данните от списъка МОДЕЛИ
		for(i = models.length-1; i>=0; i--){
			if(models.options[i].value!=0){
				models.remove(i);
			}
		}
		
		xmlhttp = getxmlhttp();
		xmlhttp.onreadystatechange = processModelsSeries;
		xmlhttp.open("GET", "ajax_soap.php?op="+index+"&manufacturer_id="+manufacturer_id+"&model_id="+model_id+"&engineId="+engine_id);
		xmlhttp.send(null);
	}
}

function processModelsSeries() {
    if(xmlhttp.readyState==4 && xmlhttp.status==200)
	{
		response = xmlhttp.responseText;
		
		if(response)
		{
			if(document.getElementById('model_series'))
				var models = document.getElementById('model_series');
			else
				var models = parent.document.getElementById('model_series');
			models.innerHTML = response;
		}
    }
}

// Engine code CASE 3
function ajaxEngineCode(index, engine_code)
{
    if(!engine_code || 0 === engine_code.length)
		alert("Моля въведете код на мотор.");
    else{
		var main_car_manufacturer = document.getElementById('main_car_manufacturer');
	
	    if(main_car_manufacturer)
			main_car_manufacturer.style.display = 'block';

		// IFrame
		var iframe = document.getElementById("iframe_result");
		iframe.contentDocument.write('<div style="margin: 0 auto; width: 130px;"><img src="images/ajax-loader.gif" /></div>');
		iframe.style.display = 'none';

	    xmlhttp = getxmlhttp();
	    xmlhttp.onreadystatechange = processEngineCode;
	    xmlhttp.open("GET", "ajax_soap.php?op="+index+"&engine_code="+encodeURIComponent(engine_code));
	    xmlhttp.send(null);
    }
}

function processEngineCode() {

	var main_car_manufacturer = document.getElementById('main_car_manufacturer');
	
		document.getElementById('iframe_result').style.display = 'none';
		main_car_manufacturer.style.display = 'block';
		
	if(xmlhttp.readyState!=4)
		main_car_manufacturer.innerHTML = '<div style="margin: 0 auto; width: 130px;"><img src="images/ajax-loader.gif" /></div>';
	else if(xmlhttp.status==200)
	{
		response = xmlhttp.responseText;
		if(response)
			main_car_manufacturer.innerHTML = response;
    }
}

// select model series to table
function ajaxSelectModelsSeries(index, manufacturer_id){
    document.getElementById('manufacturer').getElementsByTagName('option')['manufacturer['+manufacturer_id+']'].selected = 'selected';
    ajaxModelsSeries(index, manufacturer_id, 0, 0);
}

// CASE 2 - display model results
function ajaxShowResults(index, manufacturer_id, model_id, engine_id){
	xmlhttp = getxmlhttp();
	
	// var port = window.location.protocol;
	// var host = window.location.hostname;
	// var path = window.location.pathname;
	// var directory = path.split('/');
	// var url = port+"//"+host+"/"+directory[1];

	// Table all manufacturer
	if(check_login)
	{
		if(document.getElementById('main_car_manufacturer'))
			document.getElementById('main_car_manufacturer').style.display = 'none';

		// Select option loading models series
		ajaxModelsSeries(1, manufacturer_id, model_id, engine_id);

		// Loading images
		document.getElementById('divIFrame').innerHTML = '<div style="margin: 0 auto; width: 130px;"><img src="images/ajax-loader.gif" /></div>';
		document.getElementById('iframe_result').setAttribute("src", "ajax_soap.php?op="+index+"&manufacturer_id="+manufacturer_id+"&model_id="+model_id+"&engineId="+engine_id);
		document.getElementById('iframe_result').style.display = 'block';
	}
}

function mouseOver(elm){
    elm.style.cursor='pointer';
    elm.style.color='#ff0000';
}

function mouseOut(elm){
    elm.style.cursor='normal';
    elm.style.color='#000000';
}

// history info CASE 5
function loadHistory(id){
	
	var manufacturer = parent.document.getElementById('manufacturer');
		manufacturer.selectedIndex = 0;
			
	var model_series = parent.document.getElementById('model_series');
		removeSelectOption(model_series);

		document.getElementById('main_car_manufacturer').style.display = 'none';
		document.getElementById('iframe_result').style.display = 'block';		
	
		if(id){
			xmlhttp = getxmlhttp();
			xmlhttp.onreadystatechange = processDestroyHistory;
			xmlhttp.open("GET", "ajax_soap.php?op=5&id="+id);
			xmlhttp.send(null);
		}
}

function processDestroyHistory() {

	if(xmlhttp.readyState==4 && xmlhttp.status==200)
	{
		response = xmlhttp.responseText;
		
		if(response){
			// var port = window.location.protocol;
			// var host = window.location.hostname;
			// var path = window.location.pathname;
			// var directory = path.split('/');
			// var url = port+"//"+host+"/"+directory[1];
			
			var arrID = JSON.parse(response);
			var user_history_id = arrID[0];
			var manufacturer_id = arrID[1];
			var model_id = arrID[2];
			var product_id = arrID[3];
			
				// Engine
				// if(model_id==0)
					// document.getElementById('iframe_result').setAttribute("src", url+'/product_groups_engine.php?id='+product_id+'&manufacturer_id='+manufacturer_id);
				// else
			document.getElementById('iframe_result').setAttribute("src", 'product_groups.php?id='+product_id+'&manufacturer_id='+manufacturer_id+'&model_id='+model_id);				
		}
	}
}


// function loadArticleGroupTree(id, manufacturer_id){
	// var port = window.location.protocol;
	// var host = window.location.hostname;
	// var path = window.location.pathname;
	// var directory = path.split("/");
	// var url = port+"//"+host+"/"+directory[1];
	
		// // Table all manufacturer
		// document.getElementById("main_car_manufacturer").style.display = "none";
		// document.getElementById("iframe_result").style.display = "block";
		// document.getElementById("iframe_result").setAttribute("src", 'product_groups_engine.php?id='+id+'&manufacturer_id='+manufacturer_id);
// }

// History - CASE 4
function ajaxLoadHistory(manufacturer_id, model_id, product_id)
{
	var model_history = parent.document.getElementById('model_history');
	var i = 0;

	// Изчистване на данните от списъка МОДЕЛИ
	for(i = model_history.length-1; i>=0; i--){
		if(model_history.options[i].value!=0){
			model_history.remove(i);
		}
	}
	
	var textManufacturer = parent.document.getElementById('manufacturer').options['manufacturer['+manufacturer_id+']'].text;
	
		if(textManufacturer) {
			xmlhttp = getxmlhttp();
			xmlhttp.onreadystatechange = processLoadHistory;
			xmlhttp.open("GET", "ajax_soap.php?op=4&textmanufacturer="+encodeURI(textManufacturer)+"&manufacturer_id="+manufacturer_id+"&model_id="+model_id+"&product_id="+product_id);
			xmlhttp.send(null);
		}
}

function processLoadHistory() {

	if(xmlhttp.readyState==4 && xmlhttp.status==200)
	{
		response = xmlhttp.responseText;
		
		if(response)
		{
			var model_history = parent.document.getElementById('model_history');
			
			var urlsplit = response.split('||');	
				model_history.innerHTML = urlsplit[1];
				
				window.location.href = 'product_groups.php'+urlsplit[0];
		}
    }
}

// // CASE 6
// function ajaxLoadHistoryEngine(manufacturer_id, model_id, product_id)
// {
// 	var model_history = parent.document.getElementById('model_history');
// 	var i = 0;
// 	
// 	// Изчистване на данните от списъка МОДЕЛИ
// 	for(i = model_history.length-1; i>=0; i--){
// 		if(model_history.options[i].value!=0){
// 			model_history.remove(i);
// 		}
// 	}
// 	
// 	var textManufacturer = parent.document.getElementById('manufacturer').options['manufacturer['+manufacturer_id+']'].text;
// 	
// 		if(textManufacturer) {
// 			xmlhttp = getxmlhttp();
// 			xmlhttp.onreadystatechange = processLoadHistory;
// 			xmlhttp.open("GET", "ajax_soap.php?op=6&manufacturer_id="+manufacturer_id+"&product_id="+product_id);
// 			xmlhttp.send(null);
// 		}
// }


function removeSelectOption(element){
	var i = 0;

	for(i = element.length-1; i>=0; i--){
		if(element.options[i].value!=0){
			element.remove(i);
		}
	}
}


// CASE 7
function ajaxManufacturerLoadModels(index, manufacturer_id){
	xmlhttp = getxmlhttp();
	// var port = window.location.protocol;
	// var host = window.location.hostname;
	// var path = window.location.pathname;
	// var directory = path.split('/');
	// var url = port+"//"+host+"/"+directory[1];
	if(check_login)
	{
		if(document.getElementById('manufacturer'))
		{
			document.getElementById('main_car_manufacturer').style.display = 'block';
			document.getElementById('iframe_result').style.display = 'none';
			document.getElementById('iframe_result').contentDocument.write('');
			document.getElementById('manufacturer').getElementsByTagName('option')['manufacturer['+manufacturer_id+']'].selected = 'selected';
		}
		else
		{
			parent.document.getElementById('main_car_manufacturer').style.display = 'block';
			parent.document.getElementById('iframe_result').style.display = 'none';
			parent.document.getElementById('iframe_result').contentDocument.write('');
			parent.document.getElementById('manufacturer').getElementsByTagName('option')['manufacturer['+manufacturer_id+']'].selected = 'selected';
		}
		xmlhttp.onreadystatechange = processManufacturerLoadModels;
		xmlhttp.open("GET", "ajax_soap.php?op=7&manufacturer_id="+manufacturer_id);
		xmlhttp.send(null);
	}
}


function processManufacturerLoadModels() {

	if(xmlhttp.readyState!=4)
	{
		if(document.getElementById('main_car_manufacturer'))
			document.getElementById('main_car_manufacturer').innerHTML = '<div style="margin: 0 auto; width: 130px;"><img src="images/ajax-loader.gif" /></div>';
		else
			parent.document.getElementById('main_car_manufacturer').innerHTML = '<div style="margin: 0 auto; width: 130px;"><img src="images/ajax-loader.gif" /></div>';
	}
	else if(xmlhttp.status == 200){
		response = xmlhttp.responseText;

		if(response){
			if(document.getElementById('main_car_manufacturer'))
				document.getElementById('main_car_manufacturer').innerHTML = response;
			else
				parent.document.getElementById('main_car_manufacturer').innerHTML = response;
			if(document.getElementById('manufacturer'))
				var fieldElement = document.getElementById('manufacturer');
			else
				var fieldElement = parent.document.getElementById('manufacturer');
		    var manufacturer_id = fieldElement.options[fieldElement.selectedIndex].value;

			// Select option loading models series
			ajaxModelsSeries(1, manufacturer_id, 0, 0);
		}
	}
}


// CASE 8
function ajaxManufacturerYearLoadModels(index, manufacturer_id, year){
    document.getElementById('manufacturer').getElementsByTagName('option')['manufacturer['+manufacturer_id+']'].selected = 'selected';

    xmlhttp = getxmlhttp();
    xmlhttp.onreadystatechange = processManufacturerYearLoadModels;
    xmlhttp.open("GET", "ajax_soap.php?op=8&manufacturer_id="+manufacturer_id+"&year="+year);
    xmlhttp.send(null);
}


function processManufacturerYearLoadModels() {

	if(xmlhttp.readyState!=4)
		document.getElementById('main_car_manufacturer').innerHTML = '<div style="margin: 0 auto; width: 130px;"><img src="images/ajax-loader.gif" /></div>';
	else if(xmlhttp.status==200){
		response = xmlhttp.responseText;

		if(response){
		    document.getElementById('main_car_manufacturer').innerHTML = response;

		    var fieldElement = document.getElementById('manufacturer');
		    var manufacturer_id = fieldElement.options[fieldElement.selectedIndex].value;

			// Select option loading models series
			ajaxModelsSeries(1, manufacturer_id, 0, 0);
		}
	}
}

function submitViaEnter(evt, num, valueCode) {
    evt = (evt) ? evt : event;
    var charCode = (evt.charCode) ? evt.charCode : ((evt.which) ? evt.which : evt.keyCode);
    if (charCode == 13) {
        ajaxEngineCode(num, valueCode);
        return false;
    }
    return true;
}